import com.qf.repo.bean.Manager;
import com.qf.repo.db.DBUtils;
import com.qf.repo.db.PstCallBack;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        //调用查询方法
        query();
    }
    public static void query(){
        //1.sql语句
        String sql = "select id,account,password,phone,level,remark from manager";
        //2.执行查询操作
       ResultSet resultSet = DBUtils.query(sql, new PstCallBack() {
            @Override
            public void pstSetValue(PreparedStatement pst) {
                //编写 对pst中的？进行数值设置的语句
               //因为上面1.的sql语句没有？，所以不需要调用pst.pstSetValue方法
            }
        });
       //3.处理数据
        List<Manager> list = new ArrayList<>();
        try {
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String account = resultSet.getString("account");
                String password = resultSet.getString("password");
                String phone = resultSet.getString("phone");
                String level = resultSet.getString("level");
                String remark = resultSet.getString("remark");

                Manager manager = new Manager(id,account,password,phone,level,remark);
                list.add(manager);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("获取的数据条数，list = " + list.size());
        //4.关闭连接
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        DBUtils.close();
    }

    public static void update(){
        //1、sql语句
        String sql = "insert into manager(account,password,phone,level,remark) values(?,?,?,?,?)";
        //2、调用执行
        Manager admin= new Manager();
        admin.setAccount("admin");
        admin.setPassword("11111111");
        admin.setPhone("13167582311");
        admin.setLevel("level2");
        admin.setRemark("二级管理员");
        //匿名内部类充当回调函数
        int result = DBUtils.executeUpdate(sql, new PstCallBack() {
            @Override
            public void pstSetValue(PreparedStatement pst) {
                try {
//                    pst.setString(1,"root");
//                    pst.setString(2,"123456");
//                    pst.setString(3,"13167582311");
//                    pst.setString(4,"level3");
//                    pst.setString(5,"超级管理员");
                    pst.setString(1,admin.getAccount());
                    pst.setString(2,admin.getPassword());
                    pst.setString(3, admin.getPhone());
                    pst.setString(4,admin.getLevel());
                    pst.setString(5, admin.getRemark());
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        System.out.println("result = " + result);
        //关闭数据库
        DBUtils.close();
    }
}
